﻿<?php

class ShoppingCartController extends HomeController
{
	public function actionView()
	{
		$cart = Shop::getCartContent();               
		$this->render('view',array('products'=>$cart));
	}
	public function actionGetPriceTotal() {
		echo Shop::getPriceTotal();
	}
	public function actionUpdateAmount() {
		$cart = Shop::getCartContent();

		foreach($_GET as $key => $value) {
			if(substr($key, 0, 7) == 'amount_') {
				if($value == '')
					return true;
				if (!is_numeric($value) || $value <= 0)
					throw new CException('Wrong amount');
				$position = explode('_', $key);
				$position = $position[1];
				
				if(isset($cart[$position]['amount']))
					$cart[$position]['amount'] = $value;
					$product = Products::model()->findByPk($cart[$position]['product_id']);
					echo Shop::priceFormat(
							@$product->getPrice($cart[$position]['Variations'], $value));
					return Shop::setCartContent($cart);
			}	
		}

}	
	public function actionCreate()
	{
		if(Yii::app()->request->isAjaxRequest){
			$post['id']=$_POST['pid'];
			$post['amount']=1;
			$cart = Shop::getCartContent();			
			$cart[] = $post;
			Shop::setCartcontent($cart);
			echo '1';
			Yii::app()->end();
		}else{
                    
			if(!is_numeric($_POST['amount']) || $_POST['amount'] <= 0) {
				
                                Yii::app()->user->setFlash('yiishop',Yii::t('main','Không thêm được vào giỏ hàng'));			
                                $url = $this->createUrlProductDetail($_POST['id']);
                                $this->redirect($url);
			}
		
			$cart = Shop::getCartContent();	
			// remove potential clutter
			if(isset($_POST['yt0']))
				unset($_POST['yt0']);
			if(isset($_POST['yt1']))
				unset($_POST['yt1']);			
			$cart[] = $_POST;
                        
			Shop::setCartcontent($cart);
			Yii::app()->user->setFlash('yiishop',Yii::t('main','Sản phẩm đã được thêm vào giỏ hàng'));			
			$url = $this->createUrlProductDetail($_POST['id']);
			$this->redirect($url);
		}
	}
	public function actionDelete($id)
	{
		$id = (int) $id;
		$cart = json_decode(Yii::app()->user->getState('cart'), true);               
	        unset($cart[0]);
		Yii::app()->user->setState('cart', json_encode($cart));
		$this->redirect(array('//shoppingCart/view'));
	}
	public function actionIndex()
	{
		if(isset($_SESSION['cartowner'])) {
			$carts = ShoppingCart::model()->findAll('cartowner = :cartowner', array(':cartowner' => $_SESSION['cartowner']));
			$this->render('index',array( 'carts'=>$carts,));
		} 
	}	
	public function actionReload()
	{
		$products = Shop::getCartContent();
		$this->renderPartial('_cart', array(
				'products'=>$products
				), false, false);
	}
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=ShoppingCart::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='shopping cart-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
